package util;

import java.util.Date;

import dao.DBColumn;
import dao.DBTable;

public class TransformUtil {
	
	public static String PATH="/Users/send2ocean/com/hello/web";
	
	public static String PACKAGE="dyhsct";
	
	public static String DATA_TYPE="oracle";
	
	public static boolean ISDWZ=true;
	
	public static String dataType2JdbcType(String dataType,String subDataType){
		String jdbcType="void";
		if(DATA_TYPE.equals("oracle")){
			if(dataType.equals("NUMBER")){
				jdbcType="NUMERIC";
			}else if(dataType.equals("DATE")){
				jdbcType="DATE";
			}else if(dataType.equals("TIMESTAMP")){
				jdbcType="TIMESTAMP";
			}else if(dataType.equals("VARCHAR2")){
				jdbcType="VARCHAR";
			}else if(dataType.equals("BLOB")){
				jdbcType="BLOB";
			}
		}else if(DATA_TYPE.equals("mysql")){
			if(dataType.equals("INT")){
				jdbcType="NUMERIC";
			}else if(dataType.equals("INT UNSIGNED")){
				jdbcType="NUMERIC";
			}else if(dataType.equals("BIGINT")){
				jdbcType="NUMERIC";
			}else if(dataType.equals("DECIMAL")){
				jdbcType="NUMERIC";
			}else if(dataType.equals("DATE")){
				jdbcType="TIMESTAMP";
			}else if(dataType.equals("DATETIME")){
				jdbcType="TIMESTAMP";
			}else if(dataType.equals("TEXT")){
				jdbcType="VARCHAR";
			}else if(dataType.equals("VARCHAR")){
				jdbcType="VARCHAR";
			}else if(dataType.equals("BLOB")){
				jdbcType="BLOB";
			}
		}
		return jdbcType;
	}

	public static String dataType2JavaType(String dataType,String subDataType){
		String javaType="void";
		if(DATA_TYPE.equals("oracle")){
			if("NUMBER".equals(dataType)){
				if(subDataType==null || subDataType.equals("")){
					javaType="Long";
				}else{
					javaType=subDataType;
				}
			}else if("DATE".equals(  dataType)  ){
				javaType="Date";
			}else if("TIMESTAMP".equals(dataType)){
				javaType="Date";
			}else if("VARCHAR2".equals(dataType)){
				javaType="String";
			}else if("BLOB".equals(dataType )){
				javaType="byte[]";
			}
		}else if(DATA_TYPE.equals("mysql")){
			if("INT".equals(dataType)){
				javaType="Long";
			}else if("INT UNSIGNED".equals(dataType)){
				javaType="Long";
			}else if("BIGINT".equals(dataType)){
				javaType="Long";
			}else if("DECIMAL".equals(dataType)){
				javaType="Double";
			}else if("DATE".equals(  dataType)  ){
				javaType="Date";
			}else if("DATETIME".equals(dataType)){
				javaType="Date";
			}else if("TEXT".equals(dataType)){
				javaType="String";
			}else if("VARCHAR".equals(dataType)){
				javaType="String";
			}else if("BLOB".equals(dataType )){
				javaType="byte[]";
			}
		}
		return javaType;
	}
	
	public static String getLowerPrimaryKey( DBTable dbTable){
		for(DBColumn col :dbTable.getDbColumns()){
			if(col!=null && col.isPrimaryKey()) {
				return  TransformUtil.transformFieldName(col.getColumnName());
			}
		}
		return null;
	}
	
	public static String getUpperPrimaryKey(DBTable dbTable){
		for(DBColumn col :dbTable.getDbColumns()){
			if(col!=null && col.isPrimaryKey()) {
				String fieldName=TransformUtil.transformFieldName(col.getColumnName());
				StringBuilder sb=new StringBuilder(fieldName);
				sb.setCharAt(0, Character.toTitleCase(fieldName.charAt(0)));
				String methodName=sb.toString();
				return  methodName;
			}
		}
		return null;
	}
	 
	public static String getPrimaryJavaType(DBTable table){
		String javaType="void";
		if( table != null){
			for(DBColumn col:table.getDbColumns()){
				if(col.isPrimaryKey()){
					javaType=dataType2JavaType(col.getColumnType(),null);
					break;
				}else{
					continue;
				}
			}
		}
		return javaType;
	}
	
	public static String transformClassName(String str){
		return camelNameTransform(str,"_",true);
	}
	
	public static String transformFieldName(String str){
		return camelNameTransform(str,"_",false);
	}
	
	public static String transformPackageName(String str){
		str=str.replace("$", ".");
		return "com."+PACKAGE+".web."+str;
	}

	public static String camelNameTransform(String str, String sign, boolean firstUpper) {
		String returnStr="";
		str=str.toLowerCase();
		String[] arr=str.split(sign);
		if(firstUpper){
			StringBuilder sbStr=new StringBuilder(arr[0]);
			sbStr.setCharAt(0, Character.toTitleCase(arr[0].charAt(0)));
			returnStr+=sbStr.toString();
		}else{
			returnStr+=arr[0];
		}
		for (int i=1;i<arr.length;i++) {
			StringBuilder sbStr=new StringBuilder(arr[i]);
			sbStr.setCharAt(0, Character.toTitleCase(arr[i].charAt(0)));
			returnStr+=sbStr.toString();
		}
		return returnStr;
	}
	
	public static String path2Package(String filePath){
		String packPath = filePath.substring(filePath.indexOf("/")+1, filePath.length());
		packPath = packPath.replace("/", ".");
		return packPath;
	}
}
